package com.meij.activity.user;

import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.anno.ActionBarSet;
import com.anno.ActionBarSet.WAY;
import com.meij.MJApplaction;
import com.meij.R;
import com.meij.activity.MjActivity;
import com.meij.http.MJNetCallBack;
import com.meij.http.Url;

@ActionBarSet(title = "成为职业客户", way = WAY.TITLE, homeAsUpEnabled = true)
public class ChangeRoleActivity extends MjActivity {
	private int roleApply = 0;
	private String clubBelongs;
	private int canOut = 2;
	private String fee;
	// 摇晃提示动画
	private Animation shake;
	// 控件
	private EditText edite1, edite2;
	private RadioGroup radioGroup1, radioGroup2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_changerole);
		initView();
	}

	/***
	 * 初始化UI
	 */
	private void initView() {
		shake = AnimationUtils.loadAnimation(this, R.anim.shake);
		edite1 = (EditText) findViewById(R.id.editText1);
		edite2 = (EditText) findViewById(R.id.editText2);
		radioGroup1 = (RadioGroup) findViewById(R.id.rg1);
		radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb1:
					roleApply = 2;
					break;
				case R.id.rb2:
					roleApply = 3;
					break;
				case R.id.rb3:
					roleApply = 23;
					break;
				}
			}
		});
		radioGroup2 = (RadioGroup) findViewById(R.id.rg2);
		radioGroup2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb4:
					canOut = 1;
					break;
				case R.id.rb5:
					canOut = 0;
					break;
				}
			}
		});
	}

	/***
	 * 信息校验
	 * 
	 * @return
	 */
	private boolean checkInfo() {
		clubBelongs = edite1.getText().toString();
		if (TextUtils.isEmpty(clubBelongs)) {
			Toast(R.string.clubhit);
			edite1.requestFocus();
			edite1.startAnimation(shake);
			return false;
		}
		fee = edite2.getText().toString();
		if (TextUtils.isEmpty(fee)) {
			Toast(R.string.feehit);
			edite2.requestFocus();
			edite2.startAnimation(shake);
			return false;
		}
		if (roleApply == 0) {
			Toast("请选择职业类别");
			return false;
		}
		if (canOut == 2) {
			Toast("请选择可否外出");
			return false;
		}
		return true;
	}

	public void submit(View view) {
		if (checkInfo()) {
			String userKeyStr = "?userKey=" + MJApplaction.userInfo.getUserkey();
			String roleApplyStr = "&roleApply=" + roleApply;
			String clubBelongsStr = "&clubBelongs=" + clubBelongs;
			String canOutStr = "&canOut=" + canOut;
			String feeStr = "&fee=" + fee;
			String url = Url.role_apply + userKeyStr + roleApplyStr + clubBelongsStr + canOutStr + feeStr;
			MJNetCallBack callBack = new MJNetCallBack(this) {
				@Override
				public void onSuccess(JSONObject object) {
					super.onSuccess(object);
					Toast(R.string.success);
					finish();
				}
			};
			getJson(url, callBack);
		}
	}
}
